package draw;

import gui.Window;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.bbn.openmap.Environment;
import com.bbn.openmap.I18n;
import com.bbn.openmap.InformationDelegator;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.CSpecialist.GraphicPackage.RenderType;
import com.bbn.openmap.graphicLoader.AbstractGraphicLoader;
import com.bbn.openmap.gui.OMToolComponent;
import com.bbn.openmap.omGraphics.GraphicAttributes;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.tools.drawing.DrawingTool;
import com.bbn.openmap.tools.drawing.DrawingToolRequestor;
import com.bbn.openmap.tools.drawing.EditToolLoader;
import com.bbn.openmap.tools.drawing.OMDrawingTool;
import com.bbn.openmap.tools.drawing.OMDrawingToolLauncher;
import com.bbn.openmap.tools.drawing.OMDrawingToolLauncher.LoaderHolder;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.util.PaletteHelper;

public class DrawPoint extends OMToolComponent implements ActionListener,PropertyChangeListener{
	
	protected I18n i18n = Environment.getI18n();
	
	protected DrawingTool drawingTool;
	
	protected static final String creation = "com.bbn.openmap.omGraphics.OMPoint";
	
	protected DrawingToolRequestor requestor;
	
	protected GraphicAttributes ga;
	
	public DrawPoint(Window window){
		super();
		
		requestor = (DrawingToolRequestor) window.getMapHandler().get(DrawingToolRequestor.class);
		ga = initDefaultGraphicAtributes();
		
		resetGUI();
	}
	
	private GraphicAttributes initDefaultGraphicAtributes() {
		GraphicAttributes g = new GraphicAttributes();
		
		g.setFillPaint(Color.RED);
		g.setSelectPaint(Color.ORANGE);
		g.setLineType(GraphicAttributes.LINETYPE_STRAIGHT);
		g.setRenderType(RenderType._RT_LatLon);
		
		return g;
	}

	public void resetGUI() {
	        removeAll();

	        JButton addNode = new JButton(getButtonIcon("point"));
	        addNode.setActionCommand("ADD_NODE");
	        addNode.addActionListener(this);
	        
	        this.add(addNode);
	}

	public void propertyChange(PropertyChangeEvent e) {
		if (e.getPropertyName() == OMDrawingTool.LoadersProperty) {
            findLoader(((Vector) e.getNewValue()).iterator());
//            resetGUI(); loader add;
        }
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		System.out.println(command);
		
        if ("ADD_NODE".equals(command)) {
            // Get the active EditToolLoader
            DrawingTool dt = drawingTool;

            if (dt instanceof OMDrawingTool) {
                OMDrawingTool omdt = (OMDrawingTool) dt;

                if (omdt.isActivated()) {
                    omdt.deactivate();
                }
            }

            if (dt != null && creation != null
                    && requestor != null) {
                dt.setBehaviorMask(OMDrawingTool.DEFAULT_BEHAVIOR_MASK);
                dt.create(creation, ga, requestor); 
            } else {

                StringBuffer sb = new StringBuffer();
                StringBuffer em = new StringBuffer();

                if (dt == null) {
                    sb.append("   No drawing tool is available!\n");
                    em.append(i18n.get(OMDrawingToolLauncher.class,
                            "noDrowingTool",
                            "   No drawing tool is available!\n"));
                } else {
                    sb.append("   Drawing tool OK.\n");
                }

                if (creation == null) {
                    sb.append("   No valid choice of graphic to create.\n");
                    em.append(i18n.get(OMDrawingToolLauncher.class,
                            "noValidChoice",
                            "   No valid choice of graphic to create.\n"));
                } else {
                    sb.append("   Graphic choice OK.\n");
                }

                if (requestor == null) {
                    sb.append("   No valid receiver for the created graphic.\n");
                    em.append(i18n.get(OMDrawingToolLauncher.class,
                            "noValidReceiver",
                            "   No valid receiver for the created graphic.\n"));
                } else {
                    sb.append("   Graphic receiver OK.\n");
                }

                System.out.println("OMDrawingToolLauncher: Something is not set:\n"
                        + sb.toString());

                MapHandler mapHandler = (MapHandler) getBeanContext();
                if (mapHandler != null) {
                    InformationDelegator id = (InformationDelegator) mapHandler.get("com.bbn.openmap.InformationDelegator");
                    if (id != null) {

                        id.displayMessage(i18n.get(OMDrawingToolLauncher.class,
                                "problem",
                                "Problem"),
                                i18n.get(OMDrawingToolLauncher.class,
                                        "problemCreatingGraphic",
                                        "Problem creating new graphic:\n")
                                        + em.toString());
                    }
                }
            }
        }
	}
	
	public void findAndInit(Object someObj) {
		if (someObj instanceof OMDrawingTool) {
			setDrawingTool((DrawingTool) someObj);
		}
		if (someObj instanceof DrawingToolRequestor) {
			findPointRequestor(someObj);
        }
	}

	public void findAndUndo(Object someObj) {
		if (someObj instanceof OMDrawingTool) {
			OMDrawingTool dt = (OMDrawingTool) someObj;
			if (dt == getDrawingTool()) {
				setDrawingTool(null);
				dt.removePropertyChangeListener(this);
			}
		}
	}
	
	private void findLoader(Iterator iterator) {
		EditToolLoader etl = null;
		
		while (iterator.hasNext()) {
			etl = (EditToolLoader) iterator.next();
//			 addLoader((EditToolLoader) iterator.next());
			if (etl != null) {
				String[] classNames = etl.getEditableClasses();
				for (int i = 0; i < classNames.length; i++) {
//					loaders.add(new LoaderHolder(etl.getPrettyName(classNames[i]), etl));
					System.out.println(etl.getPrettyName(classNames[i])+" ---- "+etl);
					String[] sss = etl.getEditableClasses();
					for(int j=0;j<sss.length;j++){
						System.out.println(sss[j]);
					}
				}
			}
		}
	}
	
	private void findPointRequestor(Object someObj) {
		DrawingToolRequestor dtr = (DrawingToolRequestor)someObj;
//		System.out.println(dtr.getName());
		if("Nodes".equals(dtr.getName())){
			this.requestor = dtr;
		}
	}
	
	public void setDrawingTool(DrawingTool dt){
		if (drawingTool != null && drawingTool instanceof OMDrawingTool) {
			((OMDrawingTool) drawingTool).removePropertyChangeListener(this);
		}

		this.drawingTool = dt;

		if (drawingTool != null && drawingTool instanceof OMDrawingTool) {
			((OMDrawingTool) drawingTool).addPropertyChangeListener(this);
		}
	}
	
	public DrawingTool getDrawingTool(){
		return this.drawingTool;
	}
	
	private ImageIcon getButtonIcon(String iconName){
		java.net.URL imgURL = getClass().getResource("../resources/icons/"+iconName+".png");
		ImageIcon img = null;
		if (imgURL != null) 
			 img = new ImageIcon(imgURL);
		
		return img;
	}
	
}
